package com.zzh.takeoutapp.activity;

import android.content.Intent;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.config.Constants;
import com.zzh.takeoutapp.config.UrlConfig;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.ImageManager;
import com.zzh.takeoutapp.utils.LoadingUtil;
import com.zzh.takeoutapp.utils.PhoneUtils;
import com.zzh.takeoutapp.utils.SPUtils;
import com.zzh.takeoutapp.utils.ToolBarHelper;
import com.zzh.takeoutapp.utils.UtilTool;
import com.zzh.takeoutapp.utils.WxShareAndLoginUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/15.
 */

public class LoginActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.iv_showr_right)
    TextView iv_showr_right;
    @Bind(R.id.login_phone)
    AppCompatEditText login_phone;
    @Bind(R.id.login_paw)
    AppCompatEditText login_paw;
    @Bind(R.id.forget_paw)
    TextView forget_paw;
    @Bind(R.id.tv_submit)
    TextView tv_submit;
    @Bind(R.id.iv_login)
    ImageView iv_login;
    @Bind(R.id.iv_cancel)
    ImageView iv_cancel;
    @Bind(R.id.iv_cancel_password)
    ImageView iv_cancel_password;
    @Bind(R.id.me_headportrait)
    ImageView me_headportrait;
    private LoadingUtil loadingUtil;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }


    @Override
    public void initComponent() {
        iv_showr_right.setVisibility(View.VISIBLE);
        iv_showr_right.setText("注册");
        iv_showr_right.setTextSize(15);
        loadingUtil = new LoadingUtil(this);
        ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "登录");
    }

    @Override
    public void setListener() {

        String string = SPUtils.getString(this, Constants.FACE, "");
        if (string != null && string.length() > 5) {
            ImageManager.loadUrlImage(this, string, me_headportrait);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        String mobile = SPUtils.getString(this, Constants.mobile, "");
        String passwd = SPUtils.getString(this, Constants.passwd, "");
        if (mobile.length() > 5) {
            login_phone.setText(mobile);
            login_paw.setText(passwd);
        }
    }

    private String getStr(AppCompatEditText login_paw) {
        return login_paw.getText().toString().trim();
    }

    private boolean isBool() {
        if (PhoneUtils.phoneVerify(this, getStr(login_phone)) == false) {
            return false;
        } else if (getStr(login_paw) == null || getStr(login_paw).equals("")) {
            UtilTool.toast(this, "密码不能为空");
            return false;
        }
        return true;
    }

    @Override
    public void onAfterInit() {
    }

    @OnClick({R.id.iv_showr_right, R.id.forget_paw, R.id.iv_login, R.id.tv_submit, R.id.iv_cancel, R.id.iv_cancel_password})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_cancel:
                login_phone.setText("");
                break;
            case R.id.iv_cancel_password:
                login_paw.setText("");
                break;
            case R.id.iv_showr_right:
                start_Activity(new Intent(this, RegisterActivity.class));
                break;
            case R.id.forget_paw:
                start_Activity(new Intent(this, ForgetPawActivity.class));
                break;
            case R.id.iv_login:
                WxShareAndLoginUtils.WxLogin();
                break;
            case R.id.tv_submit:
                if (isBool() == true) {
                    loadingUtil.startShowLoading();
                    ServiceUrl.LoginService(this,
                            getStr(login_phone)
                            , getStr(login_paw),
                            "", "LOGIN_SERVICE");
                }
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }

    @Override
    public void onStop() {
        loadingUtil.stopShowLoading();
        EventbusHelper.unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case "LOGIN_SERVICE":
                loadingUtil.stopShowLoading();
                UtilTool.toast(this, jsonObject.optString("message"));
                if (jsonObject.optString("error").equals("0")) {
                    JSONObject object = jsonObject.optJSONObject("data");
                    SPUtils.saveString(this, Constants.mobile, getStr(login_phone));
                    SPUtils.saveString(this, Constants.passwd, getStr(login_paw));
                    SPUtils.saveString(this, Constants.TOKEN, object.optString("token"));
                    Intent intent = new Intent(this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    if (getIntent().getType().equals("OrderListFragment")) {
                        intent.setType("OrderListFragment");
                    } else if (getIntent().getType().equals("MineFragment")) {
                        intent.setType("MineFragment");
                    }
                    start_Activity(intent);
                    finish_Activity();
                }
                break;
        }
    }

}
